package com.py.credit.ai.client;

import java.util.HashMap;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baidu.aip.ocr.AipOcr;
import com.google.common.collect.Maps;
import com.py.credit.ai.constant.AipFaceConstant;
import com.py.credit.ai.res.IdCardResult;
import com.py.credit.ai.res.LicenseResult;
import com.py.credit.common.ServiceException;

@Component
public class AiOrcAdapter implements IAipOrc {

	@Autowired
	AipOcr client;

	@Override
	public IdCardResult idcard(byte[] image, String idCardSide) {

		HashMap<String, String> options = Maps.newHashMapWithExpectedSize(1);
		options.put("detect_direction", "true");
		JSONObject data = client.idcard(image, idCardSide, options);

		IdCardResult idCardResult = null;
		if (data.has("error_code")) {

			throw new ServiceException(data.getString("error_msg"));

		} else {

			String imageStatus = data.getString("image_status");

			if (!"reversed_side".equals(imageStatus) && !"non_idcard".equals(imageStatus)) {

				JSONObject wordsResult = data.getJSONObject("words_result");

				if (AipFaceConstant.ID_CARD_SIDE_FRONT.equals(idCardSide)) {

					idCardResult = IdCardResult.builder().name(wordsResult.getJSONObject("姓名").getString("words"))
							.sex(wordsResult.getJSONObject("性别").getString("words"))
							.birthday(wordsResult.getJSONObject("出生").getString("words"))
							.nation(wordsResult.getJSONObject("民族").getString("words"))
							.address(wordsResult.getJSONObject("住址").getString("words"))
							.number(wordsResult.getJSONObject("公民身份号码").getString("words")).imageStatus(imageStatus)
							.build();

				} else if (AipFaceConstant.ID_CARD_SIDE_BACK.equals(idCardSide)) {

					idCardResult = IdCardResult.builder()
							.expiration(wordsResult.getJSONObject("失效日期").getString("words")).imageStatus(imageStatus)
							.build();

				}

			} else {

				idCardResult = IdCardResult.builder().imageStatus(imageStatus).build();

			}

		}
		return idCardResult;
	}

	@Override
	public LicenseResult businessLicense(byte[] image) {
		HashMap<String, String> options = Maps.newHashMapWithExpectedSize(1);
		options.put("detect_direction", "true");
		JSONObject data = client.businessLicense(image, options);

		LicenseResult licenseResult = null;
		if (data.has("error_code")) {

			throw new ServiceException(data.getString("error_msg"));

		} else {

			JSONObject wordsResult = data.getJSONObject("words_result");
			licenseResult = LicenseResult.builder()
					.firmName(wordsResult.getJSONObject("单位名称").getString("words").replace("无", ""))
					.firmType(wordsResult.getJSONObject("类型").getString("words").replace("无", ""))
					.address(wordsResult.getJSONObject("地址").getString("words").replace("无", ""))
					.legalPerson(wordsResult.getJSONObject("法人").getString("words").replace("无", ""))
					.licenseNumber(wordsResult.getJSONObject("证件编号").getString("words").replace("无", ""))
					.limitDate(wordsResult.getJSONObject("有效期").getString("words").replace("无", ""))
					.uscCode(wordsResult.getJSONObject("社会信用代码").getString("words").replace("无", "")).build();

		}

		return licenseResult;
	}

}
